import { Component, OnInit, Input } from '@angular/core';
import { ActivatedRoute, Router, NavigationEnd } from '@angular/router';
import { HistoryService, TypeToTarget, LogWebTarget } from '../history.service';
import {
  map,
  switchMap,
  first,
  filter,
  startWith,
  distinct,
  distinctUntilChanged,
} from 'rxjs/operators';
import * as _ from 'lodash';

@Component({
  selector: 'm-history',
  templateUrl: './history.component.html',
  styleUrls: ['./history.component.scss'],
})
export class HistoryComponent implements OnInit {
  constructor(
    private route: ActivatedRoute,
    private router: Router,
    private historyService: HistoryService,
  ) {}
  @Input() type: 'zone' | 'personnel' | 'software';
  @Input() length = 10;
  TargetType = LogWebTarget;

  result$ = this.router.events.pipe(
    filter((e) => e instanceof NavigationEnd),
    startWith(null),
    map(() => {
      const query = this.route.snapshot.queryParamMap;
      return Number(query.get('id'));
    }),
    switchMap((id) => {
      return this.historyService.log_web({ target_type: TypeToTarget[this.type], target_id: id });
    }),
  );

  ngOnInit() {
    
  }
}
